﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenPhotoAlbum.Controls;
using System.Globalization;

namespace OpenPhotoAlbum
{
    public partial class Root : System.Web.UI.MasterPage
    {
        private const string ID_ANONYMOUSE_MESSAGE = "ltrAnonymouseMessage";
        private const string ID_LOGIN_STATUS_1 = "loginStatus1";
		private const string ID_OR = "ltrOr";
        private const string ID_JOIN = "hlJoin";

		private const string ID_LOGGEDIN_MESSAGE = "ltrLoggedInMessage";
		private const string ID_SETTINGS = "hlSettings";
		private const string ID_LINE = "ltrLine";
		private const string ID_LOGIN_STATUS_2 = "loginStatus2";

		public const string COOKIE_LANG = "lang";

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

			ltrLocation.Text = StringUtility.AddColon(Resources.Strings.CurrentLocation);

			if (!BusinessLogics.UserManager.Instance.IsUserLoggedIn)
			{
				phAnonymouse.Visible = true;
				phLoggedIn.Visible = false;

				ltrAnonymouseMessage.Text = Resources.Strings.LoginView_AnonymousMessage;

				loginStatus1.LoginText = Resources.Strings.LoginView_LogIn;

				ltrOr.Text = Resources.Strings.Or;

				hlJoin.Text = Resources.Strings.JoinNow;
				hlJoin.NavigateUrl = Links.Join;
			}
			else
			{
				phAnonymouse.Visible = false;
				phLoggedIn.Visible = true;

				ltrLoggedInMessage.Text = StringUtility.FormatAndHtmlEncode(
					Resources.Strings.LoginView_LoggedInMessage,
					HttpContext.Current.User.Identity.Name);

				hlSettings.Text = Resources.Strings.My_Settings;
				hlSettings.NavigateUrl = UrlContext.BuildMySettingsUrl(0);

				ltrLine.Text = Resources.Strings.Line;

				loginStatus2.LogoutText = Resources.Strings.LoginView_LogOut;
			}

			hlHome.NavigateUrl = Links.Default;
			hlHome.Text = Resources.Strings.SC_Home;
			string userName = HttpContext.Current.User.Identity.Name;
			if (string.IsNullOrEmpty(userName))
			{
				holderEditPublic.Visible = false;
			}
			else
			{
				holderEditPublic.Visible = true;
				hlEdit.NavigateUrl = Links.MyHome;
				hlEdit.Text = Resources.Strings.SC_Edit;
				hlPublic.NavigateUrl = UrlContext.BuildSharedHomeUrl(userName);
				hlPublic.Text = Resources.Strings.SC_Public;
			}

			string name = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;

			// enable all language buttons
			btnEnglish.Enabled = true;
			btnChinese.Enabled = true;

			// disable current language button
			if (name.Equals("zh-CN", StringComparison.InvariantCultureIgnoreCase))
			{
				btnChinese.Enabled = false;
			}
			else
			{
				// default
				btnEnglish.Enabled = false;
			}
			// texts
			btnEnglish.Text = Resources.Strings.Language_English;
			btnEnglish.CommandArgument = "en-US";
			btnChinese.Text = Resources.Strings.Language_Chinese;
			btnChinese.CommandArgument = "zh-CN";
			// events
			btnEnglish.Click += new EventHandler(btnLanuage_Click);
			btnChinese.Click += new EventHandler(btnLanuage_Click);
        }

		protected void btnLanuage_Click(object sender, EventArgs e)
		{
			// find selected language
			LinkButton btn = (LinkButton)sender;
			string language = btn.CommandArgument;

			// write cookie
			HttpCookie cookie = Request.Cookies[COOKIE_LANG];
			if (cookie == null) { cookie = new HttpCookie(COOKIE_LANG); }
			cookie.Expires = DateTime.UtcNow.AddMonths(1);
			cookie.Value = language;
			Response.Cookies.Add(cookie);

			// reload page
			Response.Redirect(Request.Url.ToString());
		}

		public SiteMapPath SiteMapPathControl { get { return this.siteMap; } }
    }
}
